package com.xiuxia.bopicturebackend.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.xiuxia.bopicturebackend.annotation.CheckAuth;
import com.xiuxia.bopicturebackend.api.aliyunai.AliYunAiApi;
import com.xiuxia.bopicturebackend.api.aliyunai.model.CreateOutPaintingTaskResponse;
import com.xiuxia.bopicturebackend.api.aliyunai.model.GetOutPaintingTaskResponse;
import com.xiuxia.bopicturebackend.api.imagesearch.ImageSearchApiFacade;
import com.xiuxia.bopicturebackend.api.imagesearch.model.ImageSearchResult;
import com.xiuxia.bopicturebackend.api.imagesearch.model.dto.SearchPictureByPictureRequestDTO;
import com.xiuxia.bopicturebackend.common.BaseResponse;
import com.xiuxia.bopicturebackend.common.DeleteRequest;
import com.xiuxia.bopicturebackend.common.ResultUtils;
import com.xiuxia.bopicturebackend.constant.UserConstant;
import com.xiuxia.bopicturebackend.exception.BusinessException;
import com.xiuxia.bopicturebackend.exception.ErrorCode;
import com.xiuxia.bopicturebackend.exception.ThrowUtils;
import com.xiuxia.bopicturebackend.manager.CosManager;
import com.xiuxia.bopicturebackend.manager.auth.SpaceUserAuthManager;
import com.xiuxia.bopicturebackend.manager.auth.StpKit;
import com.xiuxia.bopicturebackend.manager.auth.annotation.SaSpaceCheckPermission;
import com.xiuxia.bopicturebackend.manager.auth.model.SpaceUserPermissionConstant;
import com.xiuxia.bopicturebackend.model.dto.picture.*;
import com.xiuxia.bopicturebackend.model.entity.Picture;
import com.xiuxia.bopicturebackend.model.entity.Space;
import com.xiuxia.bopicturebackend.model.entity.User;
import com.xiuxia.bopicturebackend.model.enums.PictureReviewStatusEnum;
import com.xiuxia.bopicturebackend.model.vo.picture.PictureTagCategoryVO;
import com.xiuxia.bopicturebackend.model.vo.picture.PictureVO;
import com.xiuxia.bopicturebackend.service.PictureService;
import com.xiuxia.bopicturebackend.service.SpaceService;
import com.xiuxia.bopicturebackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/picture")
@Slf4j
@Api(tags = "pictureController")
public class PictureController {
    @Resource
    private PictureService pictureService;
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private SpaceService spaceService;
    @Resource
    private CosManager cosManager;
    @Resource
    private AliYunAiApi aliYunAiApi;

    private final Cache<String, String> LOCAL_CACHE = Caffeine.newBuilder()
            .initialCapacity(5) //初始化缓存
            .maximumSize(10_000L) //最大存一万条
            .expireAfterWrite(Duration.ofMinutes(5)) //缓存5分钟
            .build();
    @Autowired
    private SpaceUserAuthManager spaceUserAuthManager;

    /**
     * 上传图片 (可重新上传)
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVO> uploadPicture(@RequestPart("file") MultipartFile multipartFile, PictureUploadRequestDTO pictureUploadRequestDTO, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        PictureVO pictureVO = pictureService.uploadPicture(multipartFile, pictureUploadRequestDTO, loginUser);
        return ResultUtils.success(pictureVO);
    }

    @PostMapping("/upload/url")
    @ApiOperation(value = "通过Url上传图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVO> uploadPictureByURL(@RequestBody PictureUploadRequestDTO pictureUploadRequestDTO, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        String fileUrl = pictureUploadRequestDTO.getFileUrl();
        PictureVO pictureVO = pictureService.uploadPicture(fileUrl, pictureUploadRequestDTO, loginUser);
        return ResultUtils.success(pictureVO);
    }

    /**
     * 删除图片
     *
     * @param deleteRequest 删除的图片id
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_DELETE)
    public BaseResponse<Boolean> deletePicture(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Long pictureId = deleteRequest.getId();
        Boolean result = pictureService.deletePicture(pictureId, loginUser);
        return ResultUtils.success(result);

    }

    /**
     * 更新图片（仅管理员可用）
     *
     * @param updateRequestDTO 更新请求对象
     * @param request          请求
     * @return 更新结果
     */
    @PostMapping("/update")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新图片（仅管理员可用）")
    public BaseResponse<Boolean> updatePicture(@RequestBody PictureUpdateRequestDTO updateRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(updateRequestDTO == null || updateRequestDTO.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Picture picture = new Picture();
        BeanUtil.copyProperties(updateRequestDTO, picture);
        picture.setTags(JSONUtil.toJsonStr(updateRequestDTO.getTags()));
        pictureService.validPicture(picture);
        Long id = updateRequestDTO.getId();
        Picture oldPicture = pictureService.getById(id);
        ThrowUtils.throwIf(oldPicture == null, ErrorCode.NOT_FOUND_ERROR);
        pictureService.fillPictureParam(picture, userService.getLoginUser(request));
        boolean result = pictureService.updateById(picture);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据id获取图片（仅管理员可用）
     *
     * @param id      图片id
     * @param request 请求
     * @return 图片
     */
    @GetMapping("/get")
    @ApiOperation(value = "根据id获取图片（仅管理员可用）")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Picture> getPictureById(Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Picture picture = pictureService.getById(id);
        ThrowUtils.throwIf(picture == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(picture);
    }

    /**
     * 根据id获取图片封装类
     *
     * @param id      图片id
     * @param request 请求
     * @return 图片封装类
     */
    @GetMapping("/get/vo")
    @ApiOperation(value = "根据id获取图片封装类")
    public BaseResponse<PictureVO> getPictureVOById(Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Picture picture = pictureService.getById(id);
        ThrowUtils.throwIf(picture == null, ErrorCode.NOT_FOUND_ERROR);
        Long spaceId = picture.getSpaceId();
        Space space = null;
        if (spaceId != null) {
            boolean permission = StpKit.SPACE.hasPermission(SpaceUserPermissionConstant.PICTURE_VIEW);
            ThrowUtils.throwIf(!permission, ErrorCode.NO_AUTH_ERROR);
//            User loginUser = userService.getLoginUser(request);
//            pictureService.checkPictureAuth(loginUser, picture);
            space = spaceService.getById(spaceId);
            ThrowUtils.throwIf(space == null, ErrorCode.NOT_FOUND_ERROR, "空间不存在");
        }
        User loginUser = userService.getLoginUser(request);
        PictureVO pictureVO = pictureService.getPictureVO(picture, request);
        List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
        pictureVO.setPermissionList(permissionList);
        return ResultUtils.success(pictureVO);
    }

    /**
     * 分页获取图片列表（管理员可用）
     *
     * @param pictureQueryRequestDTO 查询请求
     * @param request                请求
     * @return 图片列表
     */
    @PostMapping("/list/page")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "分页获取图片列表（管理员可用）")
    public BaseResponse<Page<Picture>> listPictureByPage(@RequestBody PictureQueryRequestDTO pictureQueryRequestDTO, HttpServletRequest request) {
        long current = pictureQueryRequestDTO.getCurrent();
        long size = pictureQueryRequestDTO.getPageSize();
        Page<Picture> picturePage = pictureService.page(new Page<>(current, size), pictureService.getQueryWrapper(pictureQueryRequestDTO));
        return ResultUtils.success(picturePage);
    }

    /**
     * 分页获取图片列表
     *
     * @param pictureQueryRequestDTO 查询请求
     * @param request                请求
     * @return 图片列表
     */
    @PostMapping("/list/page/vo")
    @ApiOperation(value = "分页获取图片列表")
    public BaseResponse<Page<PictureVO>> listPictureVOByPage(@RequestBody PictureQueryRequestDTO pictureQueryRequestDTO, HttpServletRequest request) {
        long current = pictureQueryRequestDTO.getCurrent();
        long size = pictureQueryRequestDTO.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Long spaceId = pictureQueryRequestDTO.getSpaceId();
        if (spaceId == null) {
            pictureQueryRequestDTO.setReviewStatus(PictureReviewStatusEnum.PASS.getValue());
            pictureQueryRequestDTO.setNullSpaceId(true);
        } else {
            boolean permission = StpKit.SPACE.hasPermission(SpaceUserPermissionConstant.PICTURE_VIEW);
            ThrowUtils.throwIf(!permission, ErrorCode.NO_AUTH_ERROR);
            /*User loginUser = userService.getLoginUser(request);
            Space space = spaceService.getById(spaceId);
            ThrowUtils.throwIf(space == null, ErrorCode.NOT_FOUND_ERROR);
            ThrowUtils.throwIf(!space.getUserId().equals(loginUser.getId()), ErrorCode.NO_AUTH_ERROR);*/
        }
        Page<Picture> picturePage = pictureService.page(new Page<>(current, size), pictureService.getQueryWrapper(pictureQueryRequestDTO));
        return ResultUtils.success(pictureService.getPictureVOPage(picturePage, request));
    }

    /**
     * 获取图片列表（带本地缓存）
     *
     * @param pictureQueryRequestDTO 查询请求
     * @param request                请求
     * @return 图片列表
     */
    @PostMapping("/list/page/vo/localCache")
    @ApiOperation(value = "获取图片列表（带本地缓存）")
    public BaseResponse<Page<PictureVO>> listPictureVOByPageWithLocalCache(@RequestBody PictureQueryRequestDTO pictureQueryRequestDTO, HttpServletRequest request) {
        long current = pictureQueryRequestDTO.getCurrent();
        long size = pictureQueryRequestDTO.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        //非管理员用户只能看到审核通过的图片
        pictureQueryRequestDTO.setReviewStatus(PictureReviewStatusEnum.PASS.getValue());
        //先从缓存中获取
        String queryCondition = JSONUtil.toJsonStr(pictureQueryRequestDTO);
        String hashKey = DigestUtils.md5DigestAsHex(queryCondition.getBytes());
        String cacheKey = "listPictureVOByPageWithCache:" + hashKey;
        if (StrUtil.isNotBlank(LOCAL_CACHE.getIfPresent(cacheKey))) {
            Page<PictureVO> page = JSONUtil.toBean(LOCAL_CACHE.getIfPresent(cacheKey), Page.class);
            return ResultUtils.success(page);
        }
        Page<Picture> picturePage = pictureService.page(new Page<>(current, size), pictureService.getQueryWrapper(pictureQueryRequestDTO));
        Page<PictureVO> pictureVOPage = pictureService.getPictureVOPage(picturePage, request);
        LOCAL_CACHE.put(cacheKey, JSONUtil.toJsonStr(pictureVOPage));
        return ResultUtils.success(pictureVOPage);
    }

    /**
     * 获取图片列表（带redis缓存）
     *
     * @param pictureQueryRequestDTO 查询请求
     * @param request                请求
     * @return 图片列表
     */
    @PostMapping("/list/page/vo/redisCache")
    @ApiOperation(value = "获取图片列表（带redis缓存）")
    public BaseResponse<Page<PictureVO>> listPictureVOByPageWithCache(@RequestBody PictureQueryRequestDTO pictureQueryRequestDTO, HttpServletRequest request) {
        long current = pictureQueryRequestDTO.getCurrent();
        long size = pictureQueryRequestDTO.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        //非管理员用户只能看到审核通过的图片
        pictureQueryRequestDTO.setReviewStatus(PictureReviewStatusEnum.PASS.getValue());
        //先从缓存中获取
        String queryCondition = JSONUtil.toJsonStr(pictureQueryRequestDTO);
        String hashKey = DigestUtils.md5DigestAsHex(queryCondition.getBytes());
        String redisKey = "picture:listPictureVOByPageWithCache:" + hashKey;
        if (StrUtil.isNotBlank(stringRedisTemplate.opsForValue().get(redisKey))) {
            Page<PictureVO> page = JSONUtil.toBean(stringRedisTemplate.opsForValue().get(redisKey), Page.class);
            return ResultUtils.success(page);
        }
        Page<Picture> picturePage = pictureService.page(new Page<>(current, size), pictureService.getQueryWrapper(pictureQueryRequestDTO));
        Page<PictureVO> pictureVOPage = pictureService.getPictureVOPage(picturePage, request);
        int cacheExpireTime = 300 + RandomUtil.randomInt(0, 300);
        stringRedisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(pictureVOPage), cacheExpireTime, TimeUnit.SECONDS);
        return ResultUtils.success(pictureVOPage);
    }

    /**
     * 获取图片列表（多级缓存）
     *
     * @param pictureQueryRequestDTO 查询请求
     * @param request                请求
     * @return 图片列表
     */
    @PostMapping("/list/page/vo/moreCache")
    @ApiOperation(value = "获取图片列表（多级缓存）")
    public BaseResponse<Page<PictureVO>> listPictureVOByPageWithMoreCache(@RequestBody PictureQueryRequestDTO pictureQueryRequestDTO, HttpServletRequest request) {
        long current = pictureQueryRequestDTO.getCurrent();
        long size = pictureQueryRequestDTO.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        //非管理员用户只能看到审核通过的图片
        pictureQueryRequestDTO.setReviewStatus(PictureReviewStatusEnum.PASS.getValue());
        String queryCondition = JSONUtil.toJsonStr(pictureQueryRequestDTO);
        String hashKey = DigestUtils.md5DigestAsHex(queryCondition.getBytes());
        String cacheKey = "listPictureVOByPageWithCache:" + hashKey;
        //1.先从本地缓存中获取
        if (StrUtil.isNotBlank(LOCAL_CACHE.getIfPresent(cacheKey))) {
            Page<PictureVO> page = JSONUtil.toBean(LOCAL_CACHE.getIfPresent(cacheKey), Page.class);
            return ResultUtils.success(page);
        }
        //2.本地缓存没有从redis缓存中获取
        String redisKey = "picture:listPictureVOByPageWithCache:" + hashKey;
        if (StrUtil.isNotBlank(stringRedisTemplate.opsForValue().get(redisKey))) {
            Page<PictureVO> page = JSONUtil.toBean(stringRedisTemplate.opsForValue().get(redisKey), Page.class);
            return ResultUtils.success(page);
        }
        //3.redis缓存没有从数据库中获取
        Page<Picture> picturePage = pictureService.page(new Page<>(current, size), pictureService.getQueryWrapper(pictureQueryRequestDTO));
        Page<PictureVO> pictureVOPage = pictureService.getPictureVOPage(picturePage, request);
        int cacheExpireTime = 300 + RandomUtil.randomInt(0, 300);
        stringRedisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(pictureVOPage), cacheExpireTime, TimeUnit.SECONDS);
        LOCAL_CACHE.put(cacheKey, JSONUtil.toJsonStr(pictureVOPage));
        return ResultUtils.success(pictureVOPage);
    }

    /**
     * 编辑图片
     *
     * @param pictureEditRequest 编辑请求
     * @param request            请求
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPicture(@RequestBody PictureEditRequestDTO pictureEditRequest, HttpServletRequest request) {
        pictureService.editPicture(pictureEditRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 图片标签分类
     *
     * @return 图片标签分类
     */
    @GetMapping("/tag_category")
    @ApiOperation(value = "图片标签分类")
    public BaseResponse<PictureTagCategoryVO> listPictureTagCategory() {
        PictureTagCategoryVO pictureTagCategoryVO = new PictureTagCategoryVO();
        List<String> tagList = Arrays.asList("热门", "搞笑", "生活", "高清", "艺术", "校园", "背景", "简历", "创意");
        List<String> categoryList = Arrays.asList("模板", "电商", "表情包", "素材", "海报");
        pictureTagCategoryVO.setTagList(tagList);
        pictureTagCategoryVO.setCategoryList(categoryList);
        return ResultUtils.success(pictureTagCategoryVO);
    }

    /**
     * 审核图片
     */
    @PostMapping("/review")
    @ApiOperation(value = "审核图片(仅管理员可用)")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> doPictureReview(@RequestBody PictureReviewRequestDTO pictureReviewRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(pictureReviewRequestDTO == null || pictureReviewRequestDTO.getId() <= 0, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        pictureService.doPictureReview(pictureReviewRequestDTO, loginUser);
        return ResultUtils.success(true);
    }

    /**
     * 批量抓取图片
     */
    @PostMapping("/upload/batch")
    @ApiOperation(value = "批量抓取图片")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Integer> pictureUploadByBatch(@RequestBody PictureUploadByBatchRequestDTO pictureUploadByBatchRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(pictureUploadByBatchRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Integer pictureCount = pictureService.pictureUploadByBatch(pictureUploadByBatchRequestDTO, loginUser);
        return ResultUtils.success(pictureCount);
    }

    /**
     * 以图搜图
     */
    @PostMapping("/search/picture")
    @ApiOperation(value = "以图搜图")
    public BaseResponse<List<ImageSearchResult>> searchPictureByPicture(@RequestBody SearchPictureByPictureRequestDTO searchPictureByPictureRequest) {
        ThrowUtils.throwIf(searchPictureByPictureRequest == null, ErrorCode.PARAMS_ERROR);
        Long pictureId = searchPictureByPictureRequest.getPictureId();
        ThrowUtils.throwIf(pictureId == null || pictureId <= 0, ErrorCode.PARAMS_ERROR);
        Picture oldPicture = pictureService.getById(pictureId);
        ThrowUtils.throwIf(oldPicture == null, ErrorCode.NOT_FOUND_ERROR);
        List<ImageSearchResult> resultList = ImageSearchApiFacade.searchImage(oldPicture.getOriginalUrl());
        return ResultUtils.success(resultList);
    }

    /**
     * 颜色搜图
     *
     * @param searchPictureByColorRequest 颜色搜图请求
     * @param request                     请求
     * @return 颜色搜图结果
     */
    @PostMapping("/search/color")
    @ApiOperation(value = "颜色搜图")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<PictureVO>> searchPictureByColor(@RequestBody SearchPictureByColorRequestDTO searchPictureByColorRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(searchPictureByColorRequest == null, ErrorCode.PARAMS_ERROR);
        Long spaceId = searchPictureByColorRequest.getSpaceId();
        String picColor = searchPictureByColorRequest.getPicColor();
        User loginUser = userService.getLoginUser(request);
        List<PictureVO> result = pictureService.searchPictureByColor(spaceId, picColor, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 批量编辑图片
     *
     * @param pictureEditByBatchRequest 批量编辑图片请求
     * @param request                   请求
     * @return 编辑结果
     */
    @PostMapping("/edit/batch")
    @ApiOperation(value = "批量编辑图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPictureByBatch(@RequestBody PictureEditByBatchRequestDTO pictureEditByBatchRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(pictureEditByBatchRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        pictureService.batchEditPicture(pictureEditByBatchRequest, loginUser);
        return ResultUtils.success(true);
    }

    /**
     * 批量删除图片
     *
     * @param deleteRequest 批量删除图片请求
     * @param request       请求
     * @return 删除结果
     */
    @PostMapping("/delete/batch")
    @ApiOperation(value = "批量删除图片")
    public BaseResponse<Boolean> deletePictureByBatch(@RequestBody PictureDeleteByBatchRequestDTO deleteRequest, HttpServletRequest request) {
        pictureService.deletePictureByBatch(deleteRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 创建 AI 扩图任务
     */
    @PostMapping("/out_painting/create_task")
    @ApiOperation(value = "创建 AI 扩图任务")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<CreateOutPaintingTaskResponse> createPictureOutPaintingTask(
            @RequestBody CreatePictureOutPaintingTaskRequest createPictureOutPaintingTaskRequest,
            HttpServletRequest request) {
        if (createPictureOutPaintingTaskRequest == null || createPictureOutPaintingTaskRequest.getPictureId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        CreateOutPaintingTaskResponse response = pictureService.createPictureOutPaintingTask(createPictureOutPaintingTaskRequest, loginUser);
        return ResultUtils.success(response);
    }

    /**
     * 查询 AI 扩图任务
     */
    @GetMapping("/out_painting/get_task")
    @ApiOperation(value = "查询 AI 扩图任务")
    public BaseResponse<GetOutPaintingTaskResponse> getPictureOutPaintingTask(String taskId) {
        ThrowUtils.throwIf(StrUtil.isBlank(taskId), ErrorCode.PARAMS_ERROR);
        GetOutPaintingTaskResponse task = aliYunAiApi.getOutPaintingTask(taskId);
        return ResultUtils.success(task);
    }


}
